/** @file    wCore_Variant.cpp
 *  @author  Alessandro Polo
 *  @version $Id: wCore_Variant.cpp 2618 2010-06-04 09:48:15Z alex $
 *  @brief
 * File containing methods for the wosh::gui::wCore_Variant class.
 * The header for this class can be found in wCore_Variant.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include "wCore_Variant.h"

 #include <core/ObjectAllocator.h>
 #include <framework/gui/GuiUtilities.h>


using namespace std;
namespace wosh {
 namespace gui {

 WOSH_REGISTER(wosh::gui::wCore_Variant, "wosh::gui::IWidgetSerializable", 1.0, _static_wCore_Variant )
 WOSH_GUI_MAP(wosh::gui::wCore_Variant, "wosh::Variant", 0.0, _static_Map_wCore_Variant )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

wCore_Variant::wCore_Variant( QWidget* parent )
	: QWidget(parent), wEditor(NULL) {

	setWindowIcon( GuiUtilities::createIconFor( getSerializableType() ) );
	setWindowTitle( getSerializableType() );

	this->vLayout = new QVBoxLayout(this);
	this->vLayout->setContentsMargins(0,0,0,0);
	this->vLayout->setObjectName(QString::fromUtf8("verticalLayout"));
	this->cmbType = new QComboBox(this);
	this->cmbType->setModel( new VariantTypeModel() );
	this->cmbType->setCurrentIndex(0);
	connect(this->cmbType, SIGNAL(currentIndexChanged(int)), this, SLOT(changedType()));
	this->vLayout->addWidget(this->cmbType);

	resize(260, 140);
 }

WRESULT wCore_Variant::setData( const Variant& new_data ) {
	if ( this->wEditor != NULL ) {
		this->vLayout->removeWidget(this->wEditor->asQWidget());
		delete this->wEditor; this->wEditor = NULL;
	 }

	switch(new_data.getType()) {
		case Variant::BOOLEAN:	this->wEditor = new wCore_VariantCheckBox(this); break;
		case Variant::INTEGER:
		case Variant::INT64:
		case Variant::LONG:		this->wEditor = new wCore_VariantSpinBox(this); break;
		case Variant::STRING:	this->wEditor = new wCore_VariantEditor(this); break;
		default:				return WRET_ERR_INTERNAL;
	 }
	this->vLayout->addWidget(this->wEditor->asQWidget());
	setWindowTitle( new_data.getTypeAsString() );
	setToolTip( new_data.toString().c_str() );
	return this->wEditor->setData(new_data);
 }

void wCore_Variant::changedType() {
	Variant::DATA_TYPE dType = Variant::getTypeFromString( this->cmbType->currentText().toLatin1().data() );
	Variant v = this->getData();
	v.fromString(v.toString(), dType);
	setData( v );
 }


WRESULT wCore_Variant::loadObject( const wosh::Variant* object ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	return setData(*object);
 }

WRESULT wCore_Variant::saveObject( wosh::Variant* object ) const {
	if ( object == NULL ) return WRET_ERR_PARAM;
	*object = getData();
	return WRET_OK;
 }


//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

QVariant VariantTypeModel::data( const QModelIndex &index, int role ) const {
	if ( !index.isValid() )	return QVariant();

	if ( role == Qt::DisplayRole || role == Qt::EditRole ) { // cos completer!
		Variant::DATA_TYPE dType = Variant::EMPTY;
		switch(index.row()) {
			case 0: dType = Variant::EMPTY; break;
			case 1: dType = Variant::BOOLEAN; break;
			case 2: dType = Variant::CHAR; break;
			case 3: dType = Variant::INTEGER; break;
			case 4: dType = Variant::LONG; break;
			case 5: dType = Variant::INT64; break;
			case 6: dType = Variant::DATETIME; break;
			case 7: dType = Variant::TIMESPAN; break;
			case 8: dType = Variant::DOUBLE; break;
			case 9: dType = Variant::FLOAT; break;

			case 10: dType = Variant::CHARS; break;
			case 11: dType = Variant::STRING; break;

			default: break;
		 }
		return QVariant( Variant::getTypeAsString(dType, false) );
	}
	return QVariant();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////


 }; // namespace gui
}; // namespace wosh
